<?php
/**
 * 项目管理模块
 * User: lyc
 * Date: 2016/11/18
 * Time: 11:01
 */
class Involved_ItemModel extends BaseModel
{

    protected $table = 'item';

    /**
     *
     *项目列表
     */
    public function ItemList(){
        return  DB::table($this->table)
            ->orderBy('auto_id', 'desc')
            ->get();
    }


    /**
     *
     *首页新手标列表
     */
    public function NewbieItem($where,$status){
        return  DB::table($this->table)
            ->where('type',$where)
            ->whereIn('status',$status)
            ->orderBy('auto_id', 'desc')
            ->get();
    }

    /**
     *
     *单个新手标
     */
    public function NewbieItemOne($where,$status){
        return  DB::table($this->table)
            ->select('auto_id','number','type','title','amount','amount_collect','deadline','rate_loaner','begin_amount','status')
            ->where('type',$where)
            ->whereIn('status',$status)
            ->orderBy('auto_id', 'desc')
            ->take(1)
            ->first();
    }

    /**
     *
     *API首页新手标
     */
    public function ApiNewbieItem($where,$status){
        return  DB::table($this->table)
            ->select('auto_id','number','type','title','amount','amount_collect','deadline','rate_loaner','begin_amount','status')
            ->where('type',$where)
            ->whereIn('status',$status)
            ->orderBy('auto_id', 'desc')
            ->take(1)
            ->get();
    }

    //项目详细
    public function GetItem($auto_id)
    {
        $item_info = DB::table($this->table)
            ->select('item.auto_id','item.number','item.type','item.title','item.amount','item.amount_collect','item.deadline'
                ,'item.rate_loaner','item.rate_debtor','item.debtor_id','item.refund_type','item.is_pact','item.is_award','item.status'
                ,'item.begin_amount','item.safety_instructions','item.cost_description','item.add_time','item.repaying_time',
                'item.finish_time','risk_control.business_ticket_img','risk_control.user_where','risk_control.guarantee_way',
                'risk_control.repay_where','risk_control.conclusion','risk_control.risk_assessment','risk_control.prove_file')
            ->LeftJoin('risk_control','item.risk_control','=','risk_control.auto_id')
            ->where('item.auto_id',$auto_id)
            ->first();
        $item_info->add_time = date('Y-m-d H:i:s',$item_info->add_time);
        $item_info->amount =sprintf("%.2f",$item_info->amount/1000000);
        $item_info->amount_collect =sprintf("%.2f",$item_info->amount_collect/1000000);
        $item_info->is_pact = $item_info->is_pact ==1?"是":"否";
        $item_info->is_award = $item_info->is_award ==1?"是":"否";
        $item_info->business_ticket_img = " <img src={$item_info->business_ticket_img} width='330'>";
        $item_info->prove_file =" <img src={$item_info->prove_file} width='330'>";
        $item_explain = [
            'auto_id'   => '标的ID',
            'number' => '标的编号',
            'type'      => '标的类型',
            'title' => '项目名称',
            'amount'    => '项目总金额(万)',
            'amount_collect' => '以募集金额（万）',
            'deadline'  => '项目期限（天）',
            'rate_loaner'  => '债权人利率（%）',
            'rate_debtor'  => '债务人利率（%）',
            'debtor_id'  => '债务人ID',
            'refund_type'  => '还款方式',
            'is_pact'  => '是否已生成合同',
            'is_award'  => '是否发送红包',
            'status'  => '项目当前状态',
            'begin_amount'  => '起投金额（元）',
            'safety_instructions'  => '资金安全说明',
            'cost_description'  => '费用说明',
            'add_time'  => '添加时间',
            'repaying_time'  => '募集完毕时间',
            'finish_time'   =>'完成时间',
            'business_ticket_img'   =>'商票截图',
            'user_where'   =>'借款用途',
            'guarantee_way'   =>'担保方式',
            'repay_where'   =>'还款来源',
            'conclusion'   =>'结论',
            'risk_assessment'=>'风险评估',
            'prove_file'=>'证明材料'
        ];
        $data = [
            'topInfo'   => '项目ID:'.$auto_id,
            // 项目附加数据
            'item_info'      => $item_info,
            // 字段说明
            'fieldInfo' => $item_explain,
        ];
        return $data;
    }
    public function GetItemId($auto_id)
    {
        return DB::table($this->table)
            ->select()
            ->where('auto_id',$auto_id)
            ->first();
    }

    /**
     * 项目还款详情
     * @param $auto_id
     * @return array
     * @author lzhp
     */
    public function RefundItemPlan($auto_id)
    {
        $refund_info = DB::table($this->table)
            ->select('refund_item_plan.auto_id','trade_num','amount_capital','amount_loaner_accrual','amount_reality_accrual','stage','start_time'
                ,'refund_time','refund_item_plan.status')
            ->LeftJoin('refund_item_plan','refund_item_plan.item_id','=','item.auto_id')
            ->where('item_id',$auto_id)
            ->get();


        return $refund_info;
    }
    //查询单个标的状态
    public function FindItem($itemWhere){
       $status= DB::table($this->table)
            ->select('auto_id','status')
            ->where('auto_id',$itemWhere['auto_id'])
            ->where('status',$itemWhere['status'])
            ->where('is_award',$itemWhere['is_award'])
            ->first();
        return $status;
    }
    //更改红包的状态
    public function updateAward($auto_id){
        return DB::table($this->table)
            ->where('auto_id' , $auto_id)
            ->update(array('is_award' => 2));
    }
    //更改可使用站岗利息状态
    public function updateSubsidy($auto_id){
        return DB::table($this->table)
            ->where('auto_id' , $auto_id)
            ->update(array('is_subsidy' => 2));
    }
    public function FindItemSubsidy($item_id){

        return DB::table($this->table)
            ->select()
            ->where('auto_id',$item_id)
            ->where('status','REPAYING')
            ->where('is_subsidy',1)
            ->first();

    }




    /********************************************************PC端*************************************************/
    /**
     *lyc
     *我要理财
     */
    public function GetFinancing(){
        return $info = DB::table($this->table)
            ->select('auto_id','number','type','title','amount','amount_collect','deadline','rate_loaner','begin_amount','status')
            ->orderBy('add_time', 'desc')
            ->where(function($query){
                $query->where('status','RELEASE')
                    ->orWhere(function($query){
                        $query->where('status','COLLECTING')
                            ->orWhere(function($query){
                                $query->where('status','FULLSCALE')
                                    ->orWhere(function($query){
                                        $query->where('status','REPAYING');
                                    });
                            });
                    });
            })
        //    ->where('status','!=','AUDIT')
            ->paginate(10);
    }
    /**
     * lyc
     * 理财详情
     */
    public function GetFinancingDetails($paramObj){
        $info = DB::table($this->table)
            ->select('item.auto_id','item.number','item.type','item.title','item.amount','item.amount_collect','item.deadline','item.start_interest'
               ,'item.begin_amount_sx' ,'item.rate_loaner','item.rate_debtor','item.debtor_id','item.refund_type','item.is_pact','item.is_takeup','item.is_award','item.status'
                ,'item.begin_amount','item.safety_instructions','item.cost_description','item.add_time','item.repaying_time',
                'item.finish_time','risk_control.business_ticket_img','risk_control.user_where','risk_control.guarantee_way',
                'risk_control.repay_where','risk_control.conclusion','risk_control.risk_assessment','risk_control.prove_file','risk_control.prove_file1'
                ,'risk_control.prove_file2','risk_control.prove_file3','risk_control.prove_file4','risk_control.prove_file5',
                'enterprise.name','enterprise.registered_time','enterprise.registered_capital','enterprise.company_status','enterprise.brief')
            ->LeftJoin('risk_control','item.risk_control','=','risk_control.auto_id')
            ->LeftJoin('enterprise','item.debtor_id','=','enterprise.auto_id')
            ->where('item.auto_id',$paramObj->auto_id)
            ->first();
        return $info;
    }
    /**
     * 查询是否该投资
     */
    public function IsItem($auto_id){
        return $info = DB::table($this->table)
            ->select('auto_id','number','type','amount','amount_collect','rate_loaner','status','begin_amount','begin_amount_sx')
            ->where('auto_id',$auto_id)
            ->first();
    }
    public function UpdateReady($item_id){
        return  DB::table($this->table)
            ->where('auto_id',$item_id)
            ->update(array('status' => 'FULLSCALE','repaying_time'=>time()));

    }
    public function UpdateAmount($item_id,$amount){
        return  DB::table($this->table)
            ->where('auto_id',$item_id)
            ->increment('amount_collect', $amount);
    }














    /********************************************************************API**************************************************************************/
    /**
     *lyc
     *我要理财
     */
    public function GetApiFinancing($page){
        $offset = ($page-1) * HostEnum::PAGE_NUM;
            return $info = DB::table($this->table)
                ->select('auto_id','number','type','title','amount','amount_collect','deadline','rate_loaner','begin_amount','status')
                ->orderBy('add_time', 'desc')
                ->where(function($query){
                                $query->where('status','RELEASE')
                                    ->orWhere(function($query){
                                        $query->where('status','COLLECTING')
                                            ->orWhere(function($query){
                                                $query->where('status','FULLSCALE')
                                            ->orWhere(function($query){
                                                $query->where('status','REPAYING');
                                            });
                                            });
                });
                        })
                ->where('type','GENERAL')
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->get();
    }

    /**
     * lyc
     * 理财详情
     */
    public function GetApiFinancingDetails($paramObj){
        $info = DB::table($this->table)
            ->select('auto_id','type','title','amount','amount_collect','deadline','start_interest'
                ,'begin_amount_sx' ,'rate_loaner','refund_type','status','begin_amount','is_takeup','is_award')
            ->where('auto_id',$paramObj->auto_id)
            ->first();
        return $info;
    }
    /**
     * 商票
     */
    public function GetSP($paramObj){
        $info = DB::table($this->table)
            ->select('risk_control.business_ticket_img')
            ->LeftJoin('risk_control','item.risk_control','=','risk_control.auto_id')
            ->where('item.auto_id',$paramObj->auto_id)
            ->first();
        return $info;
    }
    /**
     * 材料
     */
    public function GetFile($paramObj){
        $info = DB::table($this->table)
            ->select('risk_control.prove_file','risk_control.prove_file1'
                ,'risk_control.prove_file2','risk_control.prove_file3','risk_control.prove_file4','risk_control.prove_file5')
            ->LeftJoin('risk_control','item.risk_control','=','risk_control.auto_id')
            ->where('item.auto_id',$paramObj->auto_id)
            ->first();
        return $info;
    }















    /*****************************************************************M端****************************************/
    public function GetWebFinancing(){
        return $info = DB::table($this->table)
            ->select('auto_id','number','type','title','amount','amount_collect','deadline','rate_loaner','begin_amount','status')
            ->orderBy('add_time', 'desc')
            ->where(function($query){
                $query->where('status','RELEASE')
                    ->orWhere(function($query){
                        $query->where('status','COLLECTING')
                            ->orWhere(function($query){
                                $query->where('status','FULLSCALE')
                                    ->orWhere(function($query){
                                        $query->where('status','REPAYING');
                                    });
                            });
                    });
            })
            ->where('type','GENERAL')
            ->get();
    }


}